﻿using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace Syslog_Form
{
    public partial class Form1 : Form
    {
        static string FilePath = "log.txt";
        static StreamWriter FileWriter = new StreamWriter(FilePath, true);

        public Form1()
        {
            InitializeComponent();           
        }        

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        private void UDPNetMon()
        {
            byte[] Data = new byte[1024];
            string DisData;
            IPEndPoint IP = new IPEndPoint(IPAddress.Any, 514);
            UdpClient UDPNet = new UdpClient(514);

            while (true)
            {

                DisData = Encoding.ASCII.GetString(UDPNet.Receive(ref IP));
                FileWriter.WriteLine(DisData);
                this.richTextBox1.AppendText(DisData+"\r\n");               

            }
        }

        private void SwapFile()
        {
            while (true)
            {
                this.richTextBox1.AppendText("=====Syslog文件计时器开始=====\r\n");
                Thread.Sleep(12 * 60 * 60 * 1000);
                string DecFile = @"Data\Syslog2012-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString() + "-" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + ".txt";
                File.Copy(FilePath, DecFile);
                FileWriter.Close();
                FileWriter = new StreamWriter(FilePath, false);
                this.richTextBox1.Clear();
                this.richTextBox1.AppendText("======Syslog文件备份成功======\r\n");
            }
        }

        private void TextFlush()
        {
            while (true)
            {
                Thread.Sleep(5000);
                FileWriter.Flush();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Control.CheckForIllegalCrossThreadCalls = false;

            Thread F = new Thread(UDPNetMon);
            F.IsBackground = true;
            F.Start();

            Thread W = new Thread(TextFlush);
            W.IsBackground = true;
            W.Start();

            Thread U = new Thread(SwapFile);
            U.IsBackground = true;
            U.Start();

            button1.Text = "运行中...";
            button1.Enabled = false;

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if(this.WindowState==FormWindowState.Minimized)
            {
                this.Visible=false;
                this.notifyIcon1.Visible=true;
            }
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Visible=true;
            this.WindowState=FormWindowState.Normal;
            this.Show();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Visible = true;
            this.WindowState = FormWindowState.Normal;
            this.Show();
        }

        

        
    }
}
